package com.sentinel.sentinel;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.sentinel.common.Result;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SentinelHandler {
    /**
     * @see com.sentinel.controller.UserController#getUserById
     */
    public static Result blockHandler(Long id, BlockException e) {
        Result result = null;
        if (e instanceof FlowException) {
            result = Result.failure(100, "接口被限流了");
        } else if (e instanceof DegradeException) {
            result = Result.failure(101, "服务降级了");
        } else if (e instanceof ParamFlowException) {
            result = Result.failure(102, "热点参数限流了");
        } else if (e instanceof AuthorityException) {
            result = Result.failure(104, "授权规则不通过");
        }
        log.info("sentinel 异常 ===> [{}]", result);
        return result;
    }

    /**
     *  Load {@link com.sentinel.controller.UserController#getUserById}
     */
    public static Result fallback(Long id, Throwable e) {
        return Result.failure(e.getMessage());
    }
}
